package cn.sc.allinpay.allfavour.business.cashiertrans;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import java.util.List;

import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.cashiertrsdetail.CashierTransDetailActivity;
import cn.sc.allinpay.allfavour.business.common.data.TransRecord;

/**
 * 交易流水 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class CashierTransPresenter extends PaiPresenter<CashierTransContract.View> implements CashierTransContract.Presenter {

    public static final int REQ_TRANS_DETAIL = 0x01;

    private CashierTransModel model = new CashierTransModel();
    private String inputOrderId;//输入订单号,从主界面撤销进入
    private boolean isQuerying = false;//是否正在查询，防止spinner引起的问题


    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Bundle bundle = view.getBundle();
                if (bundle != null) {
                    inputOrderId = bundle.getString(Constant.BundleKey.INPUTORDERID);
                }
            }

            @Override
            public void onStart() {
                super.onStart();
                queryTransRecords();
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (Activity.RESULT_OK == resultCode) {
                    if (REQ_TRANS_DETAIL == requestCode) {
                        queryTransRecords();
                    }
                }
            }


        };
    }

    @Override
    public void start() {
        if (!TextUtils.isEmpty(inputOrderId)) {
            view.showCondition(View.GONE);
            queryTransRecords();
        } else {
            view.showCondition(View.VISIBLE);
        }
    }

    @Override
    public void queryTransRecords() {
        if (!isQuerying) {
            isQuerying = true;
            //0：今日 1：所有
            //0：全部 1：消费 2:撤销
            view.loading("数据查询中...");
            model.queryTransRecord(view.getDate(), view.getTransType(),
                    !TextUtils.isEmpty(inputOrderId) ? inputOrderId : view.getOrderNo(),
                    new CashierTransModel.TransCallBack() {
                        @Override
                        public void onResult(List<TransRecord> resp, int size, String saleAmt) {
                            isQuerying = false;
                            view.loaded();
                            view.showTransAmt(saleAmt);
                            view.transAdapterChange(resp);
                        }
                    });
        }
    }

    @Override
    public void startTransDetailActivity(TransRecord record) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constant.BundleKey.TRANS_RECORD, record);
        view.toActivityForResult(CashierTransDetailActivity.class, bundle, REQ_TRANS_DETAIL);
    }
}
